import { NameText } from '../model/name_text'
const Sequelize = require('sequelize')
const Op = Sequelize.Op
import { IamInfo } from '../controller/iamInfo'
export class NameTextDao {
  queryNameTextList(params: any): Promise<Array<NameText>> {
    const where: any = {
      type: params.type,
      creator: params.creator
    }
    if (params.searchText) {
      where.text = { [Op.like]: '%' + params.searchText + '%' }
    }
    return NameText.findAll({
      where: where,
      limit: params.limit,
      offset: params.offset
    })
  }

  upsertNameText(params: any): Promise<any> {
    return NameText
      .findAll({
        where: params
      })
      .then((queryData: Array<NameText>) => {
        if (queryData.length === 0) {
          return NameText.upsert(params)
        } else {
          return new Promise((resolve, reject) => resolve(false))
        }
      })
  }
  deleteNameText(params: any): Promise<number> {
    return NameText.destroy({ where: { id: params.id } })
  }
  deleteAllNameText(params: any): Promise<number> {
    return NameText
      .destroy({ where: { type: params.type, creator: IamInfo.id } })
  }
}
